// SquareLine LVGL GENERATED FILE
// EDITOR VERSION: SquareLine Studio 1.1.0
// LVGL VERSION: 8.2
// PROJECT: SquareLine_Project

#include "ui.h"
#include "stm32746g_discovery_audio.h"
#include <stdio.h>
#include "main.h"
#include "fatfs.h"

#define FILENAMELENGTH 13

uint16_t inputStatus = 1;
uint16_t muteStatus = 0;
uint16_t playStatus = 0;
uint16_t musicVolume = 50;
uint16_t inputVolume = 50;

extern uint16_t musicCount;
extern char activeMusicName[FILENAMELENGTH];
extern uint8_t activeMusicIndex;
extern void readMusicViaName(char* musicName);

void toggleInputStatus(lv_event_t * e)
{
	// TOGGLE input status
	if(inputStatus) BSP_AUDIO_IN_Pause();
	else BSP_AUDIO_IN_Resume();
	// change the variable value
	inputStatus = inputStatus ? 0 : 1;
}

void toggleMuteStatus(lv_event_t * e)
{
	// TOGGLE mute status
	muteStatus = muteStatus ? 0 : 1;
  BSP_AUDIO_OUT_SetMute(muteStatus ? AUDIO_MUTE_ON : AUDIO_MUTE_OFF);
}

void PlayMusic(lv_event_t * e)
{
	// current status: PAUSE
	lv_textarea_set_text(ui_input1, "==PAUSE==");
	BSP_AUDIO_OUT_Pause();
	playStatus = 0;
}

void PauseMusic(lv_event_t * e)
{
	// current status: PLAY
	lv_textarea_set_text(ui_input1, "<<PLAY>>");
	BSP_AUDIO_OUT_Resume();
	playStatus = 1;
}

void playNextMusic(lv_event_t * e)
{
	if(activeMusicIndex < musicCount) {
		lv_textarea_set_text(ui_input1, ">>NEXT>>");
		lv_dropdown_set_selected(ui_Dropdown1, ++activeMusicIndex); // update selected music index
		lv_dropdown_get_selected_str(ui_Dropdown1, activeMusicName, FILENAMELENGTH); // update selected music name
		// same to onDropDownChange
		retSD = f_close(&SDFile);
		readMusicViaName(activeMusicName);
		BSP_AUDIO_OUT_Resume();
		playStatus = 1;
	}
}

void playLastMusic(lv_event_t * e)
{
	if(activeMusicIndex > 0) {
		lv_textarea_set_text(ui_input1, "<<PREV<<");
		lv_dropdown_set_selected(ui_Dropdown1, --activeMusicIndex);
		lv_dropdown_get_selected_str(ui_Dropdown1, activeMusicName, FILENAMELENGTH);
		// same to onDropDownChange
		retSD = f_close(&SDFile);
		readMusicViaName(activeMusicName);
		BSP_AUDIO_OUT_Resume();
		playStatus = 1;
	}
}

void onInputVolumeChange(lv_event_t * e)
{
	inputVolume = lv_slider_get_value(ui_SLIDER_INPUT);
	BSP_AUDIO_IN_SetVolume(inputVolume);
}

void onMusicVolumeChange(lv_event_t * e)
{
	musicVolume = lv_slider_get_value(ui_SLIDER_MUSIC);
	BSP_AUDIO_OUT_SetVolume(musicVolume);
}

void onDropDownChange(lv_event_t * e)
{
  // update active music name and play
	lv_dropdown_get_selected_str(ui_Dropdown1, activeMusicName, FILENAMELENGTH);
	activeMusicIndex = lv_dropdown_get_selected(ui_Dropdown1);
	
	// before read new music, close last file readed
	// Otherwise, only the first two songs can be played
	// Unknown reason >_<
	retSD = f_close(&SDFile);
	
	// read music to buffer
	readMusicViaName(activeMusicName); // use this selected name, play music

	// resume play
	BSP_AUDIO_OUT_Resume();
	
	// manually trigger click event and change UI
	if(!playStatus) {
	  lv_event_send(ui_playBtn, LV_EVENT_CLICKED, 0);
	}
}
